﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using XWord.GUI.Presenters.Interface;
using System.ComponentModel;
using Caliburn.PresentationFramework.ApplicationModel;
using System.Collections;
using System.Threading;
using XWord.WPF.ApplicationFramework;
using XWord.Common;
using XWord.Fundamental.Domain.Services;
using XWord.Fundamental.Domain;
using Microsoft.Practices.ServiceLocation;

namespace XWord.GUI.Presenters.Words
{
    public class WordExplainPresenter : Presenter, IWordExplainPresenter
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyOfPropertyChange("Name");
            }
        }

        private object _selectedItem;
        public object SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                NotifyOfPropertyChange("SelectedItem");
            }
        }

        private Hashtable _searchWord;
        public Hashtable SelectedWord
        {
            get { return _searchWord; }
            set
            {
                _searchWord = value;
                NotifyOfPropertyChange("SelectedWord");
            }
        }

        private Hashtable _data;
        public Hashtable Data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
                NotifyOfPropertyChange("Data");
            }
        }

        public string BinaryFile { get; set; }
        
        public virtual void WordChanged(string word)
        {
            Name = word;
            if (string.IsNullOrEmpty(word) || string.IsNullOrEmpty(BinaryFile)) return;
            ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            {
                SelectedItem = ServiceLocator.Current.GetInstance<IWordExplorerPresenter>().SelectedWord;
                //QueryData();
                //if (Data.ContainsKey(word))
                //{
                //    SelectedWord = Data[word] as Hashtable;
                //}
            }));
        }

        public int Order { get; set; }

        public IndexingMode Mode
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        private int _index;
        public int Index
        {
            get
            {
                return _index;
            }
            set
            {
                _index = value;
                NotifyOfPropertyChange("Index");
            }
        }

        private bool _onService=true;
        public bool OnService
        {
            get
            {
                return _onService;
            }
            set
            {
                _onService = value;
                NotifyOfPropertyChange("OnService");
               
                if (value && WordExplorerPresenter!=null && !string.IsNullOrEmpty(WordExplorerPresenter.SelectedItem))
                {
                    WordChanged(WordExplorerPresenter.SelectedItem);
                }
            }
        }

        public IWordExplorerPresenter WordExplorerPresenter { get; set; }

        protected override void OnShutdown()
        {
            base.OnShutdown();
        }

        protected string InternalBinaryFile
        {
            get
            {
                return BinaryFile;
            }
        }

        public void Update()
        {
            if (!string.IsNullOrEmpty(BinaryFile) && CanUpdate && Data!=null)
            {
                wordService.SaveBinaryFile(InternalBinaryFile, Data);
            }
        }

        public virtual void UpdateSelectWord()
        {
            return;
            if (!string.IsNullOrEmpty(BinaryFile) && CanUpdate && Data != null)
            {
                wordService.SaveSingelEntity(InternalBinaryFile, Name, SelectedWord);
                //BinaryFormatterHelper.Serialize(InternalBinaryFile, Data);
            }
        }

        public bool CanUpdate { get; set; }

        public void QueryData()
        {
            if (_data == null)
            {
                _data = wordService.GetDataFromBinaryFile(InternalBinaryFile);
                MergeTmepData();
            }
        }

        public virtual Hashtable TransFormWordToHashtable(Word word)
        {
            return null;
        }
        
        public void AddWordAction(Word word)
        {
            var ht = TransFormWordToHashtable(word);
            if (ht == null) return;
            if (Data == null)
            {
                tempHashtable[word.Name] = ht;
            }
            else
            {
                Data[word.Name] = ht;
            }
            wordService.SaveSingelEntity(this.InternalBinaryFile, word.Name, tempHashtable[word.Name]);
        }

        private void MergeTmepData()
        {
            foreach (DictionaryEntry item in tempHashtable)
            {
                Data[item.Key]=item.Value;
            }
            tempHashtable.Clear();
        }

        protected Hashtable tempHashtable = new Hashtable();


        public void RemoveWord(string word)
        {
            if (Data == null) return;
            if(Data.ContainsKey(word))
            {
                Data.Remove(word);
                wordService.SaveSingelEntity(this.InternalBinaryFile, word, null);
            }
        }

        private IWordService wordService
        {
            get { return RemotingServiceFactory.GetWordService(); }
        }
    }
}
